package com.hpe.fastfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hpe.fastfood.mapper.CommodityMapper;
import com.hpe.fastfood.pojo.Commodity;
import com.hpe.fastfood.service.CommodityService;
import com.hpe.fastfood.util.Page;
import com.hpe.fastfood.vo.CommodityVo;

@Service
public class CommodityServiceImpl implements CommodityService {

	@Autowired
	private CommodityMapper commodityMapper;

	@Override
	public Page selectCommodityByName(Page page, String foodName, int bussinessId) throws RuntimeException {
		// TODO Auto-generated method stub
		int start = (page.getCurPage() - 1) * page.getPageNumber();
		List<CommodityVo> commodity = commodityMapper.selectCommodityByName(start, page.getPageNumber(), foodName,
				bussinessId);
		int rows = commodityMapper.selectCount(foodName, bussinessId);
		int totalPage = rows % page.getPageNumber() == 0 ? rows / page.getPageNumber()
				: rows / page.getPageNumber() + 1;
		page.setDate(commodity);
		page.setRows(rows);
		page.setTotalPage(totalPage);
		return page;
	}

	@Override
	public void addCommodity(Commodity commodity) throws RuntimeException {
		// TODO Auto-generated method stub
		commodityMapper.addCommodity(commodity);
	}

	@Override
	public CommodityVo selectCommodityById(int foodId) throws RuntimeException {
		// TODO Auto-generated method stub
		return commodityMapper.selectCommodityById(foodId);
	}

	@Override
	public void updateCommodity(Commodity commodity) throws RuntimeException {
		// TODO Auto-generated method stub
		commodityMapper.updateCommodity(commodity);
	}

	@Override
	public void changeCommodity(Commodity commodity) throws RuntimeException {
		// TODO Auto-generated method stub
		commodityMapper.changeCommodity(commodity);
	}

	@Override
	public Page selectShop(Page page, String foodName, Integer typeId) throws RuntimeException {
		// TODO Auto-generated method stub
		int start = (page.getCurPage() - 1) * page.getPageNumber();
		List<CommodityVo> commodity = commodityMapper.selectShop(start, page.getPageNumber(), foodName, typeId);
		int rows = commodityMapper.selectShopCount(foodName, typeId);
		int totalPage = rows % page.getPageNumber() == 0 ? rows / page.getPageNumber()
				: rows / page.getPageNumber() + 1;
		page.setDate(commodity);
		page.setRows(rows);
		page.setTotalPage(totalPage);
		return page;
	}

	@Override
	public List<Commodity> selectCommodityByBussId(Integer bussinessId) throws RuntimeException {
		// TODO Auto-generated method stub
		return commodityMapper.selectCommodityByBussId(bussinessId);
	}

}
